/**
 * 
 */





InvoiceNamePage = {
		'BindEvent': function(){
			$('#btnSearchSubmit').bind('click', function(){
				var thirdOrderIds = $('#thirdOrderIds').val();
				if(thirdOrderIds ==''){
					return false;
				}
				
				InvoiceNamePage.BindPageInfo();
			});
			
		},
		'BindPageInfo':function(){
			var thirdOrderIds = $('#thirdOrderIds').val();
			var result = InvoiceService.UpdateInvoiceName(thirdOrderIds);
			if(result.IsSuccess == 0){
				alert(result.ErrorMsg);
			}
			InvoiceNamePage.BindTable(result.Result);
		},
		'ConvertStrNull':function(obj){
			if(obj == null){
				return '';
			}
			else{
				return obj;
			}
		},
		'BindTable': function(result){
			lst_order_en = result.datas;
			var html = '';
			for(var index in lst_order_en){
				var order_en = lst_order_en[index];
				var tr_html = "<tr>";
				
				var invoiceName ="";
				var invoiceAddress ="";
				
				tr_html += "<td>" +  order_en.third_order_id +"</td>";
				if(order_en.invoice_title !=null && order_en.invoice_title !='' && order_en.invoice_title !=undefined){
					invoiceName =  InvoiceNamePage.ConvertStrNull(order_en.invoice_title) ;
				}else{
					invoiceName =  InvoiceNamePage.ConvertStrNull(order_en.invoice_name) ;
				}
				if(order_en.invoice_address !=null && order_en.invoice_address !='' && order_en.invoice_address !=undefined){
					invoiceAddress =  InvoiceNamePage.ConvertStrNull(order_en.invoice_address) ;
				}else{
					invoiceAddress =  InvoiceNamePage.ConvertStrNull(order_en.consignee_province+order_en.consignee_city+order_en.consignee_district+order_en.consignee_address) ;
				}
				
				tr_html += "<td>" + 
				"<input type='text' id='invoiceName' style='height: 20px; width: 140px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: none; border-right-style: none; border-bottom-style: solid; border-left-style: none; border-top-color: #666666; border-right-color: #666666; border-bottom-color: #666666; border-left-color: #666666;' value='"+invoiceName+"'>" 
				
				+
				"<button id='updateOrderInvoiceTitle' orderStatus='" +order_en.invoice_status+"'"+ "thirdOrderId='"+order_en.third_order_id+"'"+ "orderId='"+order_en.order_id+"'"+"  class='btn btn-primary' style='width:60px;height:40px;font-size:10px;'>修改</button>"
				
				+"</td>";
				
				tr_html += "<td>" + 
				"<input type='text' id='invoiceAddress' style='height: 20px; width: 140px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: none; border-right-style: none; border-bottom-style: solid; border-left-style: none; border-top-color: #666666; border-right-color: #666666; border-bottom-color: #666666; border-left-color: #666666;' value='"+invoiceAddress+"'>" 
				+
				"<button id='updateOrderInvoiceAddress' orderStatus='" +order_en.invoice_status+"'"+ "thirdOrderId='"+order_en.third_order_id+"'"+"  class='btn btn-primary' style='width:60px;height:40px;font-size:10px;'>修改</button>"
				+"</td>";

				if(order_en.invoice_status ==1 || order_en.invoice_status ==14){
					tr_html += "<td> 未开" + "</td>" ;
				}else{
					tr_html += "<td> 已开" + "</td>" ;
				}
				
				tr_html += "<td>" +  InvoiceNamePage.ConvertStrNull(order_en.buyer_message)  + "</td>"
				tr_html += "<td>" + InvoiceNamePage.ConvertStrNull(order_en.seller_memo) + "</td>"
				if(order_en.invoice_kind ==21){
					tr_html += "<td> 电子" + 
					"<input type='button' id='changeToPaper' orderStatus='" +order_en.invoice_status+"'"+ "thirdOrderId='"+order_en.third_order_id+"'"+"    value='换普通纸票'/>"
					+
					"<input type='button' id='changeToSpecialPaper' orderStatus='" +order_en.invoice_status+"'"+ "thirdOrderId='"+order_en.third_order_id+"'"+"   style='background:red' value='换专票纸票'/>"
					+
					"</td>" ;
				}else{
					tr_html += "<td> 纸质" + 
					"<button id='changeToElectric' orderStatus='" +order_en.invoice_status+"'"+ "thirdOrderId='"+order_en.third_order_id+"'"+"  class='btn btn-primary' style='width:80px;height:40px;font-size:10px;'>换电子</button>"
					+
					"</td>" ;
				}
				
				tr_html += "</tr>";
				html += tr_html;
			} 
			$('#tbl_order_info tbody').html('');
			$('#tbl_order_info tbody').append(html);
			InvoiceNamePage.BindPageEvent();
		} ,
		
		'BindPageEvent': function(){
			$('#updateOrderInvoiceTitle').bind('click', function(){
				var thirdOrderId = $(this).attr('orderId');
				var orderStatus = $(this).attr('orderStatus');
				var invoiceName = $('#invoiceName').val();
				
				if(!(orderStatus =='1' || orderStatus =='14'|| orderStatus =='15')){
					alert('已开票或开票中，不可修改');
					return false;
				}
				
				var result = OrderService.UpdateInvoiceTitle(thirdOrderId,invoiceName);
				
				if(result.IsSuccess != 0){
					alert("完成修改");
				}

			});
			
			//修改发票地址
			$('#updateOrderInvoiceAddress').bind('click', function(){
				var thirdOrderId = $(this).attr('thirdOrderId');
				var invoiceAddress = $('#invoiceAddress').val();
				var result = OrderService.UpdateInvoiceAddress(thirdOrderId,invoiceAddress);
				if(result.IsSuccess == 0){
					alert("完成修改，重新查询");
				}
			});
			
			
			$('#changeToPaper').bind('click', function(){
				var thirdOrderId = $(this).attr('thirdOrderId');
				var orderStatus = $(this).attr('orderStatus');
				
				if(!(orderStatus =='1' || orderStatus =='14'|| orderStatus =='15')){
					alert('已开票或开票中，不可修改');
					return false;
				}
				
				var result = OrderService.UpdateInvoiceKind(thirdOrderId,
						22);
				
				if(result.IsSuccess != 0){
					alert("完成修改，重新查询");
				}	

			});
			
			$('#changeToSpecialPaper').bind('click', function(){
				var thirdOrderId = $(this).attr('thirdOrderId');
				var orderStatus = $(this).attr('orderStatus');
				
				if(!(orderStatus =='1' || orderStatus =='14'|| orderStatus =='15')){
					alert('已开票或开票中，不可修改');
					return false;
				}
				
				var result = OrderService.UpdateInvoiceKind(thirdOrderId,
						23);
				
				if(result.IsSuccess != 0){
					alert("完成修改，重新查询");
				}	

			});
			
			$('#changeToElectric').bind('click', function(){
				var thirdOrderId = $(this).attr('thirdOrderId');
				var orderStatus = $(this).attr('orderStatus');
				
				if(!(orderStatus =='1' || orderStatus =='14'|| orderStatus =='15')){
					alert('已开票或开票中，不可修改');
					return false;
				}
				
				var result = OrderService.UpdateInvoiceKind(thirdOrderId,
						21);
				
				if(result.IsSuccess != 0){
					alert("完成修改，重新查询");
				}

			});
			
			
		}
};

$(document).ready(function(){
	InvoiceNamePage.BindEvent();
});


